package cvc.framework.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cvc.framework.entity.CustomerMes;
import cvc.framework.entity.WebSocketMessage;
import cvc.framework.result.WebSocketResult;
import cvc.framework.webSocket.WebSocket;
import org.springframework.stereotype.Service;

@Service
public class ConnectionsService {

    public JSONArray getConnections(String targetName, String uuid) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getConnections";
        socketResult.uuid = uuid;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        JSONArray jsonArray = JSONArray.parseArray(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonArray;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getConnectionById(String targetName, String id, String uuid) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getConnectionById";
        socketResult.uuid = uuid;
        socketResult.message = id;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        JSONObject jsonSwitch = JSONObject.parseObject(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonSwitch;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int addConnection(WebSocketMessage webSocketMessage) {
        String targetName = webSocketMessage.getTargetName();
        String uuid = webSocketMessage.getUuid();
        JSONObject message = webSocketMessage.getMessage();
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "addConnection";
        socketResult.uuid = uuid;
        socketResult.message = message;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int updateConnection(WebSocketMessage webSocketMessage) {
        String targetName = webSocketMessage.getTargetName();
        String uuid = webSocketMessage.getUuid();
        JSONObject message = webSocketMessage.getMessage();
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "updateConnection";
        socketResult.uuid = uuid;
        socketResult.message = message;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteConnection(String targetName, String uuid, String id) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "deleteConnection";
        socketResult.uuid = uuid;
        socketResult.message = id;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        int code = Integer.parseInt(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return code;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public JSONArray getRelationalBrainMap(String targetName, String uuid) {
        WebSocketResult socketResult = new WebSocketResult();
        socketResult.code = "getRelationalBrainMap";
        socketResult.uuid = uuid;
        boolean ws = WebSocket.sendMessageToUser(targetName, socketResult.toString());
        try {
            if (ws) {
                int a = 0;
                while (a < 25) {    //  5秒钟超时
                    boolean contains = CustomerMes.customerMessage.containsKey(uuid);
                    if (contains) {
                        String MessageUUID = CustomerMes.customerMessage.get(uuid);
                        JSONArray jsonArray = JSONArray.parseArray(MessageUUID);
                        CustomerMes.customerMessage.remove(uuid);
                        return jsonArray;
                    }
                    Thread.sleep(200);
                    a++;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
